package com.YxyBackMange.Controller.SysController;


import com.YxyBackMange.Dto.Message;
import com.YxyBackMange.Entity.Sys.SysReUserRole;
import com.YxyBackMange.Entity.Sys.SysRole;
import com.YxyBackMange.Entity.Sys.SysUser;
import com.YxyBackMange.Mapper.Sys.SysReUserRoleMapper;
import com.YxyBackMange.Mapper.Sys.SysRoleMapper;
import com.YxyBackMange.Mapper.Sys.SysUserMapper;
import com.YxyBackMange.Service.Sys.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/Admin/Sys/SysUser")
public class SysUserController {

    private Logger logger= LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysReUserRoleMapper sysReUserRoleMapper;
    @RequestMapping("/listPage")
    public String listPage(HttpSession session, ModelMap map){

          SysRole sysRole=new SysRole();
          sysRole.setDisabled(0);
          List<SysRole> list=sysRoleMapper.select(sysRole);
          map.put("roleList",list);
          return "/admin/page/Sys/SysUser/listSysUserPage.jsp";

    }
    @RequestMapping("/addPage")
    public String addPage(HttpSession session, ModelMap map){
           SysRole sysRole=new SysRole();
           sysRole.setDisabled(0);
           List<SysRole> list=sysRoleMapper.select(sysRole);
           map.put("roleList",list);
        return "/admin/page/Sys/SysUser/updateSysUserPage.jsp";
    }
    @RequestMapping("/detailPage")
    public String detailPage(HttpSession session, @RequestParam Integer id, ModelMap map){
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUser=sysUserMapper.selectByPrimaryKey(sysUser);
        SysReUserRole sysReUserRole=new SysReUserRole();
        sysReUserRole.setUserId(sysUser.getId());
        List<SysReUserRole> userRoleList=sysReUserRoleMapper.select(sysReUserRole);
        map.put("obj",sysUser);
        map.put("userRoleList",userRoleList);
        return "/admin/page/Sys/SysUser/detailSysUserPage.jsp";

    }
    @RequestMapping("/updatePage")
    public String updatePage(HttpSession session, @RequestParam Integer id, ModelMap map){
            SysRole sysRole=new SysRole();
            sysRole.setDisabled(0);
            List<SysRole> list=sysRoleMapper.select(sysRole);
            SysUser sysUser=new SysUser();
            sysUser.setId(id);
            sysUser=sysUserMapper.selectByPrimaryKey(sysUser);
            SysReUserRole sysReUserRole=new SysReUserRole();
            sysReUserRole.setUserId(sysUser.getId());
            List<SysReUserRole> userRoleList=sysReUserRoleMapper.select(sysReUserRole);
            map.put("obj",sysUser);
            map.put("userRoleList",userRoleList);
            map.put("roleList",list);
            map.put("isUpdate",true);
        return "/admin/page/Sys/SysUser/updateSysUserPage.jsp";
    }
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public Message addOrUpdate(SysUser sysUser,@RequestParam(value = "roleId[]", required = false) Integer[] roleid,HttpSession session )
    {
        System.out.println(roleid.length);
        Message msg=new Message();
        try{
            sysUser.setRoleIdArray(roleid);
            if(sysUser!=null&&sysUser.getId()!=null)
            {
               sysUser=sysUserService.updateService(sysUser);
            }else{

                sysUser=sysUserService.insertService(sysUser);
            }
            if(sysUser==null)
            {
                msg.setMsg("保存失败,您可能没有为用户选择角色?");
                return msg;
            }
            msg.setMsg("保存成功！");
            msg.setState(true);
            logger.info("保存失败:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("保存失败，请稍后重试");
            e.printStackTrace();
            logger.error("保存失败:"+e.getMessage());
            return msg;
        }
    }
    @RequestMapping("/update")
    @ResponseBody
    public Message update(SysUser sysUser,HttpSession session)
    {
        Message msg=new Message();
        try{
            sysUserService.update(sysUser);
            msg.setData(sysUserMapper.selectByPrimaryKey(sysUser));
            msg.setMsg("更新成功！");
            msg.setState(true);
            logger.info("更新失败:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("更新失败，请稍后重试");
            logger.error("更新失败"+e.getMessage());
            e.printStackTrace();
            return msg;
        }
    }
    @RequestMapping("/delete")
    @ResponseBody
    public Message delete(@RequestParam Integer id, HttpSession session)
    {
        Message msg=new Message();
        try{
            SysUser sysUser = new SysUser();
            sysUser.setId(id);
            sysUserService.delete(sysUser);
            msg.setMsg("删除成功！");
            msg.setState(true);
            logger.info("删除成功:"+msg);
            return msg;
        }catch (Exception e)
        {
            msg.setMsg("删除失败，请稍后重试");
            logger.error("删除失败"+e.getMessage());
            e.printStackTrace();
            return msg;
        }

    }
}
